package processor;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.net.URL;
import java.net.URLClassLoader;


import request.HttpRequest;
import response.HttpResponse;
import connector.*;
import constants.Constants;

public class HttpProcessor {

	private HttpConnector connector = null;
	private HttpRequest request;

	private HttpResponse response;

	protected String method = null;
	protected String queryString = null;

	public HttpProcessor(HttpConnector connector) {
		this.connector = connector;
	}

	public void process(Socket socket) {
		try {
			request = new HttpRequest(socket.getInputStream());
			response = new HttpResponse(socket.getOutputStream());
			parseRequest(request);
			parseRequestHeaders(request);
			String uri = request.getUri();
			String requestResourceType = uri.substring(uri.lastIndexOf("/") + 1);

			
		} catch (Exception e) {

		}
	}

	public void parseRequest(HttpRequest request){
		try{
		InputStream input=request.getInputStreamN();
		
		SocketInputStream inputReader=new SocketInputStream(input,1024);
		HttpRequestLine line=new HttpRequestLine();
		inputReader.readRequestLine(line);
		char[] uri=line.uri;
		char[] method=line.method;

		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}

	public void parseRequestHeaders(HttpRequest request) throws IOException {
		InputStream input=request.getInputStreamN();
		
		SocketInputStream inputReader=new SocketInputStream(input,1024);
        boolean flag=true;
        while(flag) {
            try {
                HttpHeader header = new HttpHeader();
                inputReader.readHeader(header);
                request.getHeaders().put(header.name, header.value);
            }
            catch (Exception e){
                flag=false;
            }
        }
	}

}
